package com.loong.musicdemo.ui.find.activity.ui.detail.fragment;

import android.os.AsyncTask;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Album;
import com.loong.musicdemo.model.MV;
import com.loong.musicdemo.ui.mine.adapter.MineAlbumAdapter;
import com.loong.musicdemo.ui.mine.adapter.MineMvAdapter;
import com.loong.musicdemo.util.dataDeal.SingerDeal;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SingerMvFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SingerMvFragment extends Fragment {

    private MineMvAdapter mvAdapter;
    private RecyclerView singer_mv_recycler_view;
    private String singerId = "";
    private List<MV> mvList = new ArrayList<>();
    private TextView mv_none;
    public SingerMvFragment(String singerId) {
        this.singerId = singerId;
    }

    public static SingerMvFragment newInstance(String singerId) {
        SingerMvFragment fragment = new SingerMvFragment(singerId);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_singer_mv, container, false);
        mv_none = root.findViewById(R.id.mv_none);
        singer_mv_recycler_view = root.findViewById(R.id.singer_mv_recycler_view);
        mvAdapter = new MineMvAdapter(mvList,getContext());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        singer_mv_recycler_view.setLayoutManager(layoutManager);
        singer_mv_recycler_view.setAdapter(mvAdapter);
        new MvTask().execute();
        return root;
    }

    class MvTask extends AsyncTask<MV,Void,List<MV>>{
        @Override
        protected void onPostExecute(List<MV> mvs) {
            super.onPostExecute(mvs);
            if(mvs.size() == 0){
                mv_none.setText("抱歉！该歌手暂时没有MV！");
                mv_none.setVisibility(View.VISIBLE);
            }else {
                mvList.clear();
                mvList.addAll(mvs);
                mvAdapter.notifyDataSetChanged();
            }
        }

        @Override
        protected List<MV> doInBackground(MV... mvs) {
            return SingerDeal.getSingerMvById(singerId);
        }
    }

}